package com.example.javamaday.leetcode;

import java.util.HashMap;
import java.util.Map;

public class LeetCodeTest0242 {

    public static void main(String[] args) {

    }

    public boolean isAnagram(String s, String t) {
        boolean result = true;
        char[] sChars = s.toCharArray();
        char[] tChars = t.toCharArray();
        // 统计每个数字，出现的次数
        HashMap<Character, Integer> sMap = new HashMap<>();
        for (char sChar : sChars) {
            if (sMap.get(sChar) == null) {
                sMap.put(sChar, 1);
            } else {
                int num = sMap.get(sChar);
                sMap.put(sChar, num + 1);
            }
        }

        HashMap<Character, Integer> tMap = new HashMap<>();
        for (char tChar : tChars) {
            if (tMap.get(tChar) == null) {
                tMap.put(tChar, 1);
            } else {
                int num = tMap.get(tChar);
                tMap.put(tChar, num + 1);
            }
        }

        if (tMap.size() != sMap.size()) {
            result = false;
        } else {
            for (Map.Entry<Character, Integer> entry : tMap.entrySet()) {
                if (sMap.get(entry.getKey()) == null) {
                    result = false;
                    break;
                }
                if (!sMap.get(entry.getKey()).equals(entry.getValue())) {
                    result = false;
                    break;
                }
            }
        }

        return result;
    }
}
